const express = require('express');
const router = express.Router();
const FlowerModel = require('../model/flower');

// GET home page.
router.get('/', function(req, res, next) {
    res.send({
        code: 200,
        msg: "ok"
    });
});
// 查询花朵数据的公用方法
async function getFlowerData(req, res, next, dataType, successMsg) {
    const FlowerID = req.query.FlowerID;
    const token = req.headers.authorization.split(' ')[1]; // 去除 "Bearer " 后的 token
    console.log('收到的Token:',token);
    if (!token) {
      return res.status(401).send({
        code: 401,
        msg: '未授权访问，缺少 token'
      });
    }
    try {
        const flowerInstance = new FlowerModel(FlowerID);
        const flowerData = await flowerInstance.getFlowerData(dataType);
        res.status(200).send({
            code: 200,
            msg: successMsg,
            data: flowerData
        });
    } catch (error) {
        res.status(500).send({
            code: 500,
            msg: `数据查询失败: ${error.message}`
        });
    }
}

// 查询对应花的图片
router.get('/GetFlowerPhoto', (req, res, next) => {
    getFlowerData(req, res, next, 'FlowerPhoto', '花的图片获取成功');
});

// 查询对应花升级所需经验
router.get('/GetFlowerFullExp', (req, res, next) => {
    getFlowerData(req, res, next, 'Exp', '花升级所需经验获取成功');
});

module.exports = router;
